<template>
  <div class="container">
    <div class="content">
      <div class="topBox">
        <div class="left" v-permission="'roleManagementAdd'">
          <el-button class="btnColor" @click="addRole()">添加+</el-button>
        </div>
      </div>
      <div class="tableBox">
        <el-table v-loading="loading" :data="tableData" style="width: 100%">
          <el-table-column
            align="center"
            prop="id"
            label="角色编号"
            width="100"
          />
          <el-table-column align="center" prop="roleName" label="角色名称" />
          <el-table-column
            align="center"
            prop="dataScopeName"
            label="数据权限"
          />
          <el-table-column align="center" prop="updateTime" label="创建时间" />

          <el-table-column align="center" prop="description" label="角色描述" />
          <el-table-column
            align="center"
            prop="address"
            label="操作"
            width="260"
          >
            <template slot-scope="scope">
              <div class="btnBox">
                <div
                  slot="reference"
                  class="editBtn1"
                  @click="addRole(scope.row)"
                  v-permission="'roleManagementEdit'"
                >
                  <i class="el-icon-edit" />编辑角色
                </div>
                <div
                  slot="reference"
                  class="editBtn2"
                  @click="goAuthority(scope.row.id)"
                  v-permission="'roleManagementAuthority'"
                >
                  <i class="el-icon-edit" />分配权限
                </div>
                <div
                  v-if="scope.row.roleName !== '超级管理员'"
                  slot="reference"
                  class="editBtn3"
                  type="text"
                  @click="delDetail(scope.row)"
                  v-permission="'roleManagementDel'"
                >
                  <i class="el-icon-delete" />
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <!-- <div class="footerPage">
          <el-pagination
            :current-page="params.PageIndex"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="10"
            layout="total, sizes, prev, pager, next"
            :total="totalItemCount"
            @size-change="handleSizeChange"
            @current-change="currentPage"
          />
        </div> -->
      </div>
    </div>

    <el-dialog
      :show-close="false"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
      :close-on-click-modal="false"
    >
      <div slot="title" class="title1">
        <div class="oval" />
        <div class="msg">{{ dialogTitle }}</div>
      </div>

      <div class="formBox">
        <div class="top">请填写角色信息</div>
        <el-form ref="form" class="formItem" :model="form" label-width="80px">
          <el-form-item label="角色名称">
            <el-input v-model="form.roleName" />
          </el-form-item>

          <el-form-item label="数据权限">
            <el-select v-model="form.dataScope" placeholder="请选择数据权限">
              <el-option
                v-for="item in dataScopeList"
                :key="item.id"
                :label="item.name"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
          <el-form-item style="text-align: right">
            <el-button class="btnColor" @click="onSubmitRole"
              >立即创建</el-button
            >
            <el-button @click="dialogVisible = false">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getRoleList,
  roleUpdate,
  roleAdd,
  roleDelete,
} from "@/api/systemManagement";
import { dictionaryListApi } from "@/api/login";

export default {
  name: "AuthorityManagement",
  data() {
    return {
      dialogVisible: false,
      totalItemCount: null,
      tableData: [],
      form: {
        roleName: "",
        dataScope: "",
      },
      titleName: "权限管理",
      loading: true,
      dataScopeList: [],
      dialogTitle: "添加角色",
    };
  },
  created() {
    this.GetRoleList();
    this.dictionaryList();
  },
  methods: {
    async dictionaryList() {
      const res = await dictionaryListApi({
        type: "data_scope",
      });
      this.dataScopeList = res.data;
    },
    async GetRoleList() {
      const res = await getRoleList();
      this.tableData = res.data;
      this.loading = false;
    },

    async addRole(row) {
      this.dialogVisible = true;
      if (row.id) {
        this.form = row;
        this.dialogTitle = "编辑角色";
      } else {
        this.dialogTitle = "添加角色";
      }
    },
    handleClose(res) {
      if (res.success) {
        this.$message.success(res.msg);
      } else {
        this.$message.error(res.msg);
        return;
      }

      this.dialogVisible = false;
      this.GetRoleList();
      this.form = {
        roleName: "",
        dataScope: "",
      };
    },
    async onSubmitRole() {
      if (this.form.id) {
        const data = {
          id: this.form.id,
          roleName: this.form.roleName,
          dataScope: this.form.dataScope,
        };
        const res = await roleUpdate(data);
        if (res.code == 0) {
          this.$message({
            message: "新增成功",
            type: "success",
          });
        }
        this.GetRoleList();

        this.dialogVisible = false;
        this.form = {
          roleName: "",
          dataScope: "",
        };
      } else {
        const data = {
          roleName: this.form.roleName,
          dataScope: this.form.dataScope,
        };
        const res = await roleAdd(data);
        if (res.code == 0) {
          this.$message({
            message: "新增成功",
            type: "success",
          });
          this.GetRoleList();
        }
        this.dialogVisible = false;
        this.form = {
          roleName: "",
          dataScope: "",
        };
      }
    },
    async delDetail(row) {
      this.$confirm(`此操作将永久删除   ${row.roleName}, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await roleDelete({ id: row.id });
          if (res.code == 0) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.GetRoleList();
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    goAuthority(id) {
      this.$router.push({
        path: "/roleDistribution",
        query: { roleId: id },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  background-color: #f8fafc;
  padding: 10px;
  .btnColor {
    background-color: #0077FF;
    color: #fff;
    margin-left: 10px;
  }
  .greenColor {
    background-color: #dbf8dd;
    color: #0077FF;
    width: 80px;
    height: 25px;
    line-height: 25px;
    text-align: center;
    margin: 0 auto;
    border-radius: 25px;
    padding: 0 10px;
  }
  .redColor {
    background-color: #f7e9ea;
    color: #e11d48;
    width: 80px;
    height: 25px;
    line-height: 25px;
    text-align: center;
    margin: 0 auto;
    border-radius: 25px;
    padding: 0 10px;
  }
  .content {
    background-color: #fff;
    padding: 20px 10px;
    .topBox {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 40px;
      .right {
        display: flex;
      }
    }
    .tableBox {
      padding: 10px 40px;
      ::v-deep .el-table th,
      .el-table tr {
        background-color: #f6f9f8 !important;
        color: #333 !important;
        height: 30px;
      }
      ::v-deep .el-table__row {
        height: 50px !important;
        padding: 0 !important;
      }
      ::v-deep .el-table th,
      .el-table tr {
        height: 50px !important;
      }
      ::v-deep .el-table td,
      .el-table th {
        padding: 0 !important;
        padding-top: 5px !important;
      }

      .btnBox {
        display: flex;
        align-items: center;
        justify-content: center;

        .editBtn1 {
          cursor: pointer;
          background-color: #0077FF;
          color: #fff;
          // width: 25px;
          padding: 0 10px;

          height: 25px;
          line-height: 25px;
          text-align: center;
          border-radius: 5px;
        }
        .editBtn2 {
          cursor: pointer;
          margin-left: 10px;
          background-color: #ffa426;
          padding: 0 10px;
          color: #fff;
          height: 25px;
          line-height: 25px;
          text-align: center;
          border-radius: 5px;
        }
        .editBtn3 {
          cursor: pointer;
          margin-left: 10px;
          background-color: #d72e2e;
          width: 25px;
          color: #fff;
          height: 25px;
          line-height: 25px;
          text-align: center;
          border-radius: 5px;
        }
      }

      .footerPage {
        padding: 0 30px 0 0;
        display: flex;
        justify-content: right;
        width: 100%;
        background-color: #f8fafc;
        height: 50px;
        align-items: center;
        margin-top: 10px;

        //  ::v-deep .el-pagination .btn-next, .el-pagination .btn-prev{
        //   background-color: #087 !important;
        // }
      }
    }
  }
  ::v-deep.el-dialog__body {
    background-color: #f8fafc;
    padding: 10px;
  }

  .title1 {
    background-color: #fff;
    height: 50px;
    line-height: 50px;
    padding-left: 30px;
    display: flex;
    align-items: center;

    .oval {
      width: 80px;
      height: 10px;
      border-radius: 10px;
      background-color: #249451;
      margin-right: 20px;
    }
    .msg {
      font-size: 20px;
    }
  }

  .formBox {
    background-color: #fff;
    padding: 10px;
    .top {
      margin-bottom: 20px;
    }
    .formItem {
      padding: 0 20px;
    }
  }
}
</style>
